/***************************************************************************************
 The copyright in this software is being made available under the BSD License, included
 below. This software may be subject to other third party rights,including patent rights,
 and no such rights are granted under this license.

 Regents of the Tsinghua University, Peking University, Zhejiang University, HKUST
 Tsinghua University, Peking University, Zhejiang University, HKUST 2011

 Copyright (c) 2011, Regents of the Tsinghua University, Peking University, Zhejiang
 University, HKUST. All Rights Reserved.

 Redistribution and use in source and binary forms, with or without modification, are
 permitted provided that the following conditions are met:
   * Redistributions of source code must retain the above copyright notice,
     this list of conditions and the following disclaimer.
   * Redistributions in binary form must reproduce the above copyright notice,
     this list of conditions and the following disclaimer in the documentation
     and/or other materials provided with the distribution.
   * Neither the names of the copyright holders nor the names of its
     contributors may be used to endorse or promote products derived from this
     software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
 OF SUCH DAMAGE.
***************************************************************************************/


/**************************************************************************************
* File name: block.c
* Function: Description
*
*************************************************************************************
*/

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <memory.h>

#include "contributors.h"
#include "defines.h"
#include "global.h"
#include "block.h"
#include "vlc.h"
#include "golomb_dec.h"
#include "qcoder.h"

#define EP (edgepixels+20)


unsigned short IQ_TAB[64] =
{

    32768, 36061, 38968, 42495, 46341, 50535, 55437, 60424,
    32932, 35734, 38968, 42495, 46177, 50535, 55109, 59933,
    65535, 35734, 38968, 42577, 46341, 50617, 55027, 60097,
    32809, 35734, 38968, 42454, 46382, 50576, 55109, 60056,
    65535, 35734, 38968, 42495, 46320, 50515, 55109, 60076,
    65535, 35744, 38968, 42495, 46341, 50535, 55099, 60087,
    65535, 35734, 38973, 42500, 46341, 50535, 55109, 60097,
    32771, 35734, 38965, 42497, 46341, 50535, 55109, 60099
};

short IQ_SHIFT[64] =
{
    15, 15, 15, 15, 15, 15, 15, 15,
    14, 14, 14, 14, 14, 14, 14, 14,
    14, 13, 13, 13, 13, 13, 13, 13,
    12, 12, 12, 12, 12, 12, 12, 12,
    12, 11, 11, 11, 11, 11, 11, 11,
    11, 10, 10, 10, 10, 10, 10, 10,
    10, 9, 9, 9, 9, 9, 9, 9,
    8, 8, 8, 8, 8, 8, 8, 8
};

const int DCT_16[16][16]=
{
    {     32,    32,    32,    32,    32,    32,    32,    32,    32,    32,    32,    32,    32,    32,    32,    32  },
    {     45,    43,    40,    35,    29,    21,    13,     4,    -4,   -13,   -21,   -29,   -35,   -40,   -43,   -45  },
    {     44,    38,    25,     9,    -9,   -25,   -38,   -44,   -44,   -38,   -25,    -9,     9,    25,    38,    44  },
    {     43,    29,     4,   -21,   -40,   -45,   -35,   -13,    13,    35,    45,    40,    21,    -4,   -29,   -43  },
    {     42,    17,   -17,   -42,   -42,   -17,    17,    42,    42,    17,   -17,   -42,   -42,   -17,    17,    42  },
    {     40,     4,   -35,   -43,   -13,    29,    45,    21,   -21,   -45,   -29,    13,    43,    35,    -4,   -40  },
    {     38,    -9,   -44,   -25,    25,    44,     9,   -38,   -38,     9,    44,    25,   -25,   -44,    -9,    38  },
    {     35,   -21,   -43,     4,    45,    13,   -40,   -29,    29,    40,   -13,   -45,    -4,    43,    21,   -35  },
    {     32,   -32,   -32,    32,    32,   -32,   -32,    32,    32,   -32,   -32,    32,    32,   -32,   -32,    32  },
    {     29,   -40,   -13,    45,    -4,   -43,    21,    35,   -35,   -21,    43,     4,   -45,    13,    40,   -29  },
    {     25,   -44,     9,    38,   -38,    -9,    44,   -25,   -25,    44,    -9,   -38,    38,     9,   -44,    25  },
    {     21,   -45,    29,    13,   -43,    35,     4,   -40,    40,    -4,   -35,    43,   -13,   -29,    45,   -21  },
    {     17,   -42,    42,   -17,   -17,    42,   -42,    17,    17,   -42,    42,   -17,   -17,    42,   -42,    17  },
    {     13,   -35,    45,   -40,    21,     4,   -29,    43,   -43,    29,    -4,   -21,    40,   -45,    35,   -13  },
    {      9,   -25,    38,   -44,    44,   -38,    25,    -9,    -9,    25,   -38,    44,   -44,    38,   -25,     9  },
    {      4,   -13,    21,   -29,    35,   -40,    43,   -45,    45,   -43,    40,   -35,    29,   -21,    13,    -4  },
};


static int sign( int a, int b )
{
    int x;
    x = RFM_ABS( a );

    if ( b >= 0 )
        return x;
    else
        return -x;
}
/*
*************************************************************************
* Function:Inverse transform 8x8 block.
* Input:
* Output:
* Return:
* Attention:input curr_blk has to be [lines][pixels]
*************************************************************************
*/

void inv_transform_B8( int curr_blk[B8_SIZE][B8_SIZE] )
{
    int i, j ;
    int temp1[8], temp2[8];

    for( i=0; i<8; i++ )
    {
        // step 1
        temp1[0] = ( curr_blk[i][0] + curr_blk[i][4] )*181>>7 ;
        temp1[1] = ( curr_blk[i][0] - curr_blk[i][4] )*181>>7 ;
        temp1[2] = ( curr_blk[i][2]*196>>8 ) - ( curr_blk[i][6]*473>>8 ) ;
        temp1[3] = ( curr_blk[i][2]*473>>8 ) + ( curr_blk[i][6]*196>>8 ) ;

        temp2[4] = curr_blk[i][1] - curr_blk[i][7] ;
        temp2[7] = curr_blk[i][1] + curr_blk[i][7] ;
        temp2[5] = curr_blk[i][3]*181>>7 ;
        temp2[6] = curr_blk[i][5]*181>>7 ;
        temp1[4] = temp2[4] + temp2[6] ;
        temp1[5] = temp2[7] - temp2[5] ;
        temp1[6] = temp2[4] - temp2[6] ;
        temp1[7] = temp2[7] + temp2[5] ;

        // step 2
        temp2[0] = temp1[0] + temp1[3] ;
        temp2[3] = temp1[0] - temp1[3] ;
        temp2[1] = temp1[1] + temp1[2] ;
        temp2[2] = temp1[1] - temp1[2] ;
        temp2[4] = ( temp1[4]*301>>8 ) - ( temp1[7]*201>>8 ) ;
        temp2[7] = ( temp1[4]*201>>8 ) + ( temp1[7]*301>>8 ) ;
        temp2[5] = ( temp1[5]*710>>9 ) - ( temp1[6]*141>>9 ) ;
        temp2[6] = ( temp1[5]*141>>9 ) + ( temp1[6]*710>>9 ) ;

        // step 3
        curr_blk[i][0] = temp2[0] + temp2[7] ;
        curr_blk[i][7] = temp2[0] - temp2[7] ;
        curr_blk[i][1] = temp2[1] + temp2[6] ;
        curr_blk[i][6] = temp2[1] - temp2[6] ;
        curr_blk[i][2] = temp2[2] + temp2[5] ;
        curr_blk[i][5] = temp2[2] - temp2[5] ;
        curr_blk[i][3] = temp2[3] + temp2[4] ;
        curr_blk[i][4] = temp2[3] - temp2[4] ;
    }
    for( i=0; i<8; i++ )
    {
        // step 1
        temp1[0] = ( curr_blk[0][i] + curr_blk[4][i] )*181>>7 ;
        temp1[1] = ( curr_blk[0][i] - curr_blk[4][i] )*181>>7 ;
        temp1[2] = ( curr_blk[2][i]*196>>8 ) - ( curr_blk[6][i]*473>>8 ) ;
        temp1[3] = ( curr_blk[2][i]*473>>8 ) + ( curr_blk[6][i]*196>>8 ) ;

        temp2[4] = curr_blk[1][i] - curr_blk[7][i] ;
        temp2[7] = curr_blk[1][i] + curr_blk[7][i] ;
        temp2[5] = curr_blk[3][i]*181>>7 ;
        temp2[6] = curr_blk[5][i]*181>>7 ;
        temp1[4] = temp2[4] + temp2[6] ;
        temp1[5] = temp2[7] - temp2[5] ;
        temp1[6] = temp2[4] - temp2[6] ;
        temp1[7] = temp2[7] + temp2[5] ;

        // step 2
        temp2[0] = temp1[0] + temp1[3] ;
        temp2[3] = temp1[0] - temp1[3] ;
        temp2[1] = temp1[1] + temp1[2] ;
        temp2[2] = temp1[1] - temp1[2] ;
        temp2[4] = ( temp1[4]*301>>8 ) - ( temp1[7]*201>>8 ) ;
        temp2[7] = ( temp1[4]*201>>8 ) + ( temp1[7]*301>>8 ) ;
        temp2[5] = ( temp1[5]*710>>9 ) - ( temp1[6]*141>>9 ) ;
        temp2[6] = ( temp1[5]*141>>9 ) + ( temp1[6]*710>>9 ) ;

        // step 3
        curr_blk[0][i] = temp2[0] + temp2[7] ;
        curr_blk[7][i] = temp2[0] - temp2[7] ;
        curr_blk[1][i] = temp2[1] + temp2[6] ;
        curr_blk[6][i] = temp2[1] - temp2[6] ;
        curr_blk[2][i] = temp2[2] + temp2[5] ;
        curr_blk[5][i] = temp2[2] - temp2[5] ;
        curr_blk[3][i] = temp2[3] + temp2[4] ;
        curr_blk[4][i] = temp2[3] - temp2[4] ;

        for( j=0; j<8; j++ )
        {
            if( curr_blk[j][i]>0 )
            {
                curr_blk[j][i] = ( curr_blk[j][i] + 16 )>>5 ;
            }
            else
            {
                curr_blk[j][i] = -( ( -curr_blk[j][i] + 16 )>>5 ) ;
            }
        }
    }
}

void idct_dequant_B8( int block8x8, int qp,int curr_blk[B8_SIZE][B8_SIZE], image_t *img )
{
    int xx, yy;
    int b8_y = ( block8x8 / 2 ) << 3;
    int b8_x = ( block8x8 % 2 ) << 3;
    int curr_val;
    int b8_cy;

    inv_transform_B8( curr_blk );


    if ( block8x8 > 5 )
        b8_cy = 8;
    else
        b8_cy = 0;

    // normalize
    for ( yy = 0; yy < 8; yy++ )
        for ( xx = 0; xx < 8; xx++ )
        {
            if ( block8x8 <= 3 )
                curr_val = img->mpr[b8_x + xx][b8_y + yy] + curr_blk[yy][xx];
            else
                curr_val = img->mpr[xx][yy + b8_cy] + curr_blk[yy][xx];

            img->m7[xx][yy] = curr_blk[yy][xx] = RFM_CLIP3( curr_val, 0, 255 );

            if ( block8x8 <= 3 )
                p_img_y_dec[img->pix_y + b8_y + yy][img->pix_x + b8_x + xx] = curr_blk[yy][xx];
            else
                p_img_uv_dec[( block8x8 - 4 ) % 2][img->pix_c_y + yy + b8_cy][img->pix_c_x + xx] = curr_blk[yy][xx];

        }
}


void idct_dequant_B8inB4( int block8x8, int qp,int curr_blk[B8_SIZE][B8_SIZE], image_t *img )
{
    int xx, yy;
    int b8_y = ( block8x8 / 2 ) << 3;
    int b8_x = ( block8x8 % 2 ) << 3;
    int curr_val;
    int b8_cy;

    inv_transform_B8inB4( curr_blk,block8x8 );


    if ( block8x8 > 5 )
        b8_cy = 8;
    else
        b8_cy = 0;

    // normalize
    for ( yy = 0; yy < 8; yy++ )
        for ( xx = 0; xx < 8; xx++ )
        {
            if ( block8x8 <= 3 )
                curr_val = img->mpr[b8_x + xx][b8_y + yy] + curr_blk[yy][xx];
            else
                curr_val = img->mpr[xx][yy + b8_cy] + curr_blk[yy][xx];

            /*img->m7[xx][yy] = */
            curr_blk[yy][xx] = RFM_CLIP3( curr_val, 0, 255 );

            if ( block8x8 <= 3 )
                p_img_y_dec[img->pix_y + b8_y + yy][img->pix_x + b8_x + xx] = curr_blk[yy][xx];
            else
                p_img_uv_dec[( block8x8 - 4 ) % 2][img->pix_c_y + yy + b8_cy][img->pix_c_x + xx] = curr_blk[yy][xx];

        }
}

/*
*************************************************************************
* Function:
* Input:
* Output:
* Return:
* Attention:
*************************************************************************
*/
void reconstruct8x8( int curr_blk[8][8], int block8x8 )
{
    int yy, xx, block_y, block_x;
    int incr_y = 1, off_y = 0;

    if ( block8x8 < 4 )
    {
        block_y = ( block8x8 / 2 ) * 8;
        block_x = ( block8x8 % 2 ) * 8;

        for ( yy = 0; yy < 8; yy++ )
        {
            for ( xx = 0; xx < 8; xx++ )
            {
                p_img_y_dec[img->pix_y + block_y + incr_y * yy + off_y][img->pix_x + block_x + xx] = curr_blk[yy][xx];
            }
        }
    }
    else
    {
        for ( yy = 0; yy < 8; yy++ )
        {
            for ( xx = 0; xx < 8; xx++ )
            {
                p_img_uv_dec[block8x8 - 4][img->pix_c_y + yy][img->pix_c_x + xx] = curr_blk[yy][xx];
            }
        }
    }
}

void read_luma_coeff_b8_qcoder( int block8x8, input_t *inp, image_t *img )
{
    int i;
    int mb_nr = img->current_mb_nr;
    macroblock_t *currMB = &mb_data[mb_nr];

    int intra;
    int inumcoeff;
    int ipos;
    int ii, jj;
    int boff_x, boff_y;
    int any_coeff;
    int vlc_numcoef;
    int cbp_blk_mask;
    float QP99;
    long sum;
    int shift = 7;
    int val, QPI;
    int coeff_buf[64];
    int qp;
    int is_eob;

    intra = ( currMB->mb_type == I4MB || currMB->mb_type == I8MB );
    inumcoeff = 64;
    memset( coeff_buf, 0, 64*sizeof( int ) );

    /************************************************************************/
    /*                     8x8                                              */
    /************************************************************************/

    qp = currMB->qp;
    QP99 = ( float )( pow( 2.0, ( float )( qp - 5.0 ) / 8.0 ) );
    for ( shift = 1; shift < 16; shift++ )
    {
        QPI = ( int )( ( 1 << shift ) * QP99 + 0.5 );
        if ( QPI > ( 1 << 16 ) )
        {
            shift -= 1;
            QPI = ( int )( ( 1 << shift ) * QP99 + 0.5 );
            break;
        }
    }

    if ( shift == 16 )
        shift = 15;
    QPI = ( int )( ( 1 << shift ) * QP99 + 0.5 );

    cbp_blk_mask = cbp_blk_masks[0];
    if ( block8x8 & 1 )
        cbp_blk_mask <<= 2;
    if ( block8x8 & 2 )
        cbp_blk_mask <<= 8;
    currMB->cbp_blk &= ~cbp_blk_mask;

    vlc_numcoef = -1;
    boff_x = ( ( block8x8 % 2 ) << 3 );
    boff_y = ( ( block8x8 / 2 ) << 3 );
    img->subblock_x = boff_x >> 2;
    img->subblock_y = boff_y >> 2;

    ipos = -1;
    any_coeff = 1;
    for( i=0; i<inumcoeff; )
    {
        is_eob = qcoder_decode_eobflag_8x8( qcoder, i );
        if( !is_eob )
        {
            do
            {
                coeff_buf[i] = qcoder_decode_luma_b8x8_coeff( qcoder, i, intra );
                i++;
            }
            while( coeff_buf[i-1]==0 && i<64 );
        }
        else
            break;
    }

    vlc_numcoef = i;

    if ( vlc_numcoef != 0 )
    {
        for ( i=0; i<vlc_numcoef; i++ )
        {
            ipos = i;

            ii = SCAN[1][ipos][0];
            jj = SCAN[1][ipos][1];

            shift = IQ_SHIFT[qp];
            QPI = IQ_TAB[qp];
            val = coeff_buf[i];

            ///////intra DC prediction
            if( !ii && !jj && ( currMB->subblock_type[block8x8] == SUBBLOCK_INTRA8x8 ) )
            {
#if INTRA_MODE_ADAPTIVE
                ///local vars
                int by,bx;
                by=block8x8/2;
                bx=block8x8%2;
                if( by==1 && currMB->b8mode[by][bx]==INTRA_UP )
                {
                    val+=currMB->y_dc[by-1][bx];
                }
                else if( currMB->b8mode[by][bx]==INTRA_UP )
                {
                    val+=mb_data[img->current_mb_nr-( img->width/MB_BLOCK_SIZE )].y_dc[1][bx];
                }
                else if( bx==1 && currMB->b8mode[by][bx]==INTRA_LEFT )
                {
                    val+=currMB->y_dc[by][bx-1];
                }
                else if( currMB->b8mode[by][bx]==INTRA_LEFT )
                {
                    val+=mb_data[img->current_mb_nr-1].y_dc[by][1];
                }
                currMB->y_dc[by][bx]=val;
#else
                int by,bx;
                by=block8x8/2;
                bx=block8x8%2;
                if( bx==0 && by==0 )
                {
                    if( img->pix_x && mb_data[img->current_mb_nr-1].mb_type==I8MB )
                        val+=mb_data[img->current_mb_nr-1].y_dc[by][1];
                    else if( img->pix_y && mb_data[img->current_mb_nr-( img->width/MB_BLOCK_SIZE )].mb_type==I8MB )
                        val+=mb_data[img->current_mb_nr-( img->width/MB_BLOCK_SIZE )].y_dc[1][bx];
                }
                else if( bx==0 && by==1 )
                {
                    val+=currMB->y_dc[by-1][bx];
                }
                else if( bx==1 && by==0 )
                {
                    val+=currMB->y_dc[by][bx-1];
                }
                else if( bx==1 && by==1 )
                {
                    val+=currMB->y_dc[by][bx-1];
                }
                currMB->y_dc[by][bx]=val;
#endif
            }
            //////////////////////////
            if ( val > 2047 )
            {
                CSyntax = 0;
#if SYNTAX_REPORT
                fprintf( p_sreport,"error(1)                %8d @(%d,%d) Blk%d MB%d\n",val,jj,ii,block8x8,1/*img->picture_structure*/==0
                         ?img->current_mb_nr_fld:img->current_mb_nr );
#endif
                val = 2047;
            }
            else if ( val < -2048 )
            {
                CSyntax = 0;
#if SYNTAX_REPORT
                fprintf( p_sreport,"error(1)                %8d @(%d,%d) Blk%d MB%d\n",val,jj,ii,block8x8,1/*img->picture_structure*/==0
                         ?img->current_mb_nr_fld:img->current_mb_nr );
#endif
                val = -2048;
            }
            sum = sign( ( RFM_ABS( val ) * QPI + ( 1 << ( shift - 2 ) ) ) >> ( shift - 1 ),val );
            if ( sum > 8191 )
            {
                CSyntax = 0;
#if SYNTAX_REPORT
                fprintf( p_sreport,"error(2)                %8d @(%d,%d) Blk%d MB%d\n",sum,jj,ii,block8x8,1/*img->picture_structure*/==0
                         ?img->current_mb_nr_fld:img->current_mb_nr );
#endif
                sum = 8191;
            }
            else if ( sum < -8192 )
            {
                CSyntax = 0;
#if SYNTAX_REPORT
                fprintf( p_sreport,"error(2)                %8d @(%d,%d) Blk%d MB%d\n",sum,jj,ii,block8x8,1/*img->picture_structure*/==0
                         ?img->current_mb_nr_fld:img->current_mb_nr );
#endif
                sum = -8192;
            }
            img->m7[boff_x + ii][boff_y + jj] = sum;
        }

        if ( any_coeff )
        {
            cbp_blk_mask = cbp_blk_masks[( ( boff_y & 4 ) >> 1 ) | ( ( boff_x & 4 ) >> 2 )];
            if ( block8x8 & 1 )
                cbp_blk_mask <<= 2;
            if ( block8x8 & 2 )
                cbp_blk_mask <<= 8;
            currMB->cbp_blk |= cbp_blk_mask;
        }
    }
    else
    {
#if INTRA_MODE_ADAPTIVE
        int by,bx;
        boff_x = ( ( block8x8 % 2 ) << 3 );
        boff_y = ( ( block8x8 / 2 ) << 3 );
        shift = IQ_SHIFT[qp];
        QPI = IQ_TAB[qp];
        by=block8x8/2;
        bx=block8x8%2;
        currMB->y_dc[by][bx]=0;
        if( by==1 && currMB->b8mode[by][bx]==INTRA_UP )
        {
            currMB->y_dc[by][bx]=currMB->y_dc[by-1][bx];
        }
        else if( currMB->b8mode[by][bx]==INTRA_UP )
        {
            currMB->y_dc[by][bx]=mb_data[img->current_mb_nr-( img->width/MB_BLOCK_SIZE )].y_dc[1][bx];
        }
        else if( bx==1 && currMB->b8mode[by][bx]==INTRA_LEFT )
        {
            currMB->y_dc[by][bx]=currMB->y_dc[by][bx-1];
        }
        else if( currMB->b8mode[by][bx]==INTRA_LEFT )
        {
            currMB->y_dc[by][bx]=mb_data[img->current_mb_nr-1].y_dc[by][1];
        }
        if( currMB->b8mode[by][bx]!=INTRA_NO )
            val=currMB->y_dc[by][bx];
        else
            val=0;
        sum = sign( ( RFM_ABS( val ) * QPI + ( 1 << ( shift - 2 ) ) ) >> ( shift - 1 ),val );
        img->m7[boff_x][boff_y]=sum;
#else
        int by,bx;
        boff_x = ( ( block8x8 % 2 ) << 3 );
        boff_y = ( ( block8x8 / 2 ) << 3 );
        shift = IQ_SHIFT[qp];
        QPI = IQ_TAB[qp];
        by=block8x8/2;
        bx=block8x8%2;
        currMB->y_dc[by][bx]=0;
		if(currMB->subblock_type[block8x8] == SUBBLOCK_INTRA8x8)
		{
			if( bx==0 && by==0 )
			{
				if( img->pix_x && mb_data[img->current_mb_nr-1].mb_type==I8MB )
					currMB->y_dc[by][bx]=mb_data[img->current_mb_nr-1].y_dc[by][1];
				else if( img->pix_y && mb_data[img->current_mb_nr-( img->width/MB_BLOCK_SIZE )].mb_type==I8MB )
					currMB->y_dc[by][bx]=mb_data[img->current_mb_nr-( img->width/MB_BLOCK_SIZE )].y_dc[1][bx];
			}
			else if( bx==0 && by==1 )
			{
				currMB->y_dc[by][bx]=currMB->y_dc[by-1][bx];
			}
			else if( bx==1 && by==0 )
			{
				currMB->y_dc[by][bx]=currMB->y_dc[by][bx-1];
			}
			else if( bx==1 && by==1 )
			{
				currMB->y_dc[by][bx]=currMB->y_dc[by][bx-1];
			}
		}
        val=currMB->y_dc[by][bx];
        sum = sign( ( RFM_ABS( val ) * QPI + ( 1 << ( shift - 2 ) ) ) >> ( shift - 1 ),val );
        img->m7[boff_x][boff_y]=sum;
#endif
    }
}

void read_luma_coeff_b4_qcoder( int block8x8, int block4x4, input_t *inp, image_t *img )
{
    int i,k,m;
    int mb_nr = img->current_mb_nr;
    macroblock_t *currMB = &mb_data[mb_nr];

    int intra;
    int inumcoeff;
    int ipos;
    int ii, jj;
    int boff_x, boff_y;
    int any_coeff;
    int vlc_numcoef;
    int cbp_blk_mask;
    int b4;

    float QP99;
    long sum;
    int shift = 7;
    int val, QPI;
    int coeff_buf[64];

    int qp;

    int tmp_y_dc;
    int is_eob;
    intra = ( currMB->mb_type == I4MB || currMB->mb_type == I8MB );
    inumcoeff = 64;
    memset( coeff_buf, 0, 64*sizeof( int ) );

    /************************************************************************/
    /*                  4x4                                                 */
    /************************************************************************/
    {
        //4x4

        qp = currMB->qp_4x4; // using old style qp.

        QP99 =( float )pow( 2.0, ( float )( qp-5.0 )/8.0 );

        for ( shift=1; shift<16; shift++ )
        {
            QPI = ( int )( ( 1<<shift )*QP99+0.5 );
            if ( QPI>( 1<<16 ) )
            {
                shift -=1;
                QPI = ( int )( ( 1<<shift )*QP99+0.5 );
                break;
            }
        }

        if ( shift==16 ) shift=15;
        QPI = ( int )( ( 1<<shift )*QP99+0.5 ); // the same as 8x8

        //clear cbp_blk bits of thie 8x8 block (and not all 4!)
        cbp_blk_mask = cbp_blk_masks[0] ;
        if( block8x8&1 )cbp_blk_mask<<=2;
        if( block8x8&2 )cbp_blk_mask<<=8;
        currMB->cbp_blk&=~cbp_blk_mask;

        vlc_numcoef=-1;

        if( intra )
        {
            vlc_numcoef=0;        //this means 'use numcoeffs symbol'.
        }


        // === decoding ===    //  different with 8x8
        // === set offset in current macroblock ===
        boff_x = ( ( block8x8%2 )<<3 );
        boff_y = ( ( block8x8/2 )<<3 );
        img->subblock_x = boff_x>>2;
        img->subblock_y = boff_y>>2;

        ipos  = -1;
        any_coeff=1;   //modified by qwang    any_coeff=0

        inumcoeff = 16;
        //for( b4=0; b4<4; b4++)  //zc 4
        b4 = block4x4;
        {
            memset( coeff_buf, 0, 64*sizeof( int ) );
            for( i=0; i<inumcoeff; )
            {
                is_eob = qcoder_decode_eobflag_4x4( qcoder, i );
                if( !is_eob )
                {
                    do
                    {
                        coeff_buf[i] = qcoder_decode_luma_b4x4_coeff( qcoder, i, intra );
                        i++;
                    }
                    while( coeff_buf[i-1]==0 && i<16 );
                }
                else
                    break;
            }

            vlc_numcoef = i;

            qp = currMB->qp_4x4;
            ipos=-1;//reset
            if ( vlc_numcoef != 0 )
            {
                for( i=0 ; i<( vlc_numcoef ); i++ )
                {
                    ipos = i;

                    {
                        ii = SCAN_4x4[1][ipos][0];
                        jj = SCAN_4x4[1][ipos][1];
                    }
                    shift = IQ_SHIFT[qp]-1;
                    QPI   = IQ_TAB[qp];
                    val = coeff_buf[i];
                    //  DC prediction
                    if ( !ii && !jj &&( currMB->b4_ref[block8x8][block4x4]==INTRA ) )
                    {
#if INTRA_MODE_ADAPTIVE
                        if ( !( block8x8/2 )&&!( b4/2 )&&currMB->b4mode[block8x8/2][block8x8%2][b4/2][b4%2]==INTRA_UP )
                            val += ( mb_data[img->current_mb_nr-( img->width/MB_BLOCK_SIZE )].y_dc[1][block8x8%2] )>>1; //mb_data[img->current_mb_nr-(img->width/MB_BLOCK_SIZE)].y_dc[1][block8x8%2];
                        else if( currMB->b4mode[block8x8/2][block8x8%2][b4/2][b4%2]==INTRA_UP )
                        {
                            if ( ( block8x8/2 )&&!( b4/2 ) )

                                val += ( currMB->y_dc[0][block8x8%2] )>>1;
                            else
                                val += currMB->y_4x4_dc[block8x8/2][block8x8%2][0][b4%2];
                        }
                        if ( !( block8x8%2 )&&!( b4%2 )&&currMB->b4mode[block8x8/2][block8x8%2][b4/2][b4%2]==INTRA_LEFT )
                            val += ( mb_data[img->current_mb_nr-1].y_dc[block8x8/2][1] )>>1;
                        else if( currMB->b4mode[block8x8/2][block8x8%2][b4/2][b4%2]==INTRA_LEFT )
                        {
                            if ( ( block8x8%2 )&&!( b4%2 ) )

                                val += ( currMB->y_dc[block8x8/2][0] )>>1;
                            else
                                val += currMB->y_4x4_dc[block8x8/2][block8x8%2][b4/2][0];
                        }
                        currMB->y_4x4_dc[block8x8/2][block8x8%2][b4/2][b4%2] = val;
#else
                        switch ( block8x8*4+b4 )
                        {
                        case 0:
                            if ( img->pix_x && mb_data[img->current_mb_nr-1].mb_type==I8MB )
                            {
                                val+=mb_data[img->current_mb_nr-1].y_dc[0][1]>>1;
                            }
                            else if( img->pix_y && mb_data[img->current_mb_nr-( img->width/MB_BLOCK_SIZE )].mb_type==I8MB )
                            {
                                val+=mb_data[img->current_mb_nr-( img->width/MB_BLOCK_SIZE )].y_dc[1][0]>>1;
                            }
                            break;

						case 1:
						case 2:
						case 3:
						case 5:
						case 6:
						case 7:
						case 9:
						case 10:
						case 11:
						case 13:
						case 14:
						case 15:
							val += currMB->y_4x4_dc[block8x8/2][block8x8%2][0][0];
							break;
						case 4:
						case 8:
						case 12:
							val += currMB->y_dc[0][0]>>1;
							break;
						}
                        currMB->y_4x4_dc[block8x8/2][block8x8%2][b4/2][b4%2] = val;
#endif
                    }
                    sum = sign( ( RFM_ABS( val )*QPI+( 1<<( shift-1 ) ) )>>shift,val );
                    img->m7[boff_x+( b4%2 )*4+ii][boff_y+( b4/2 ) *4+jj]=sum;
                }
            }
            else //4x4 all zeros
            {
#if INTRA_MODE_ADAPTIVE
                boff_x = ( ( block8x8%2 )<<3 );
                boff_y = ( ( block8x8/2 )<<3 );
                shift = IQ_SHIFT[qp]-1;
                QPI   = IQ_TAB[qp];
                currMB->y_4x4_dc[block8x8/2][block8x8%2][b4/2][b4%2] = 0;
                if ( !( block8x8/2 )&&!( b4/2 )&&currMB->b4mode[block8x8/2][block8x8%2][b4/2][b4%2]==INTRA_UP )
                    currMB->y_4x4_dc[block8x8/2][block8x8%2][b4/2][b4%2] = ( mb_data[img->current_mb_nr-( img->width/MB_BLOCK_SIZE )].y_dc[1][block8x8%2] )>>1; //mb_data[img->current_mb_nr-(img->width/MB_BLOCK_SIZE)].y_dc[1][block8x8%2];
                else if( currMB->b4mode[block8x8/2][block8x8%2][b4/2][b4%2]==INTRA_UP )
                {
                    if ( ( block8x8/2 )&&!( b4/2 ) )

                        currMB->y_4x4_dc[block8x8/2][block8x8%2][b4/2][b4%2] = ( currMB->y_dc[0][block8x8%2] )>>1; //currMB->y_dc[0][block8x8%2];
                    else
                        currMB->y_4x4_dc[block8x8/2][block8x8%2][b4/2][b4%2] = currMB->y_4x4_dc[block8x8/2][block8x8%2][0][b4%2];
                }
                if ( !( block8x8%2 )&&!( b4%2 )&&currMB->b4mode[block8x8/2][block8x8%2][b4/2][b4%2]==INTRA_LEFT )
                    currMB->y_4x4_dc[block8x8/2][block8x8%2][b4/2][b4%2] = ( mb_data[img->current_mb_nr-1].y_dc[block8x8/2][1] )>>1; //mb_data[img->current_mb_nr-1].y_dc[block8x8/2][1];
                else if( currMB->b4mode[block8x8/2][block8x8%2][b4/2][b4%2]==INTRA_LEFT )
                {
                    if ( ( block8x8%2 )&&!( b4%2 ) )

                        currMB->y_4x4_dc[block8x8/2][block8x8%2][b4/2][b4%2] = ( currMB->y_dc[block8x8/2][0] )>>1; //currMB->y_dc[block8x8/2][0];
                    else
                        currMB->y_4x4_dc[block8x8/2][block8x8%2][b4/2][b4%2] = currMB->y_4x4_dc[block8x8/2][block8x8%2][b4/2][0];
                }
                if( currMB->b4mode[block8x8/2][block8x8%2][b4/2][b4%2]!=INTRA_NO )
                    val = currMB->y_4x4_dc[block8x8/2][block8x8%2][b4/2][b4%2];
                else
                    val = 0;
                sum = sign( ( RFM_ABS( val )*QPI+( 1<<( shift-1 ) ) )>>shift,val );
                img->m7[boff_x+( b4%2 )*4][boff_y+( b4/2 ) *4]=sum;
#else
				boff_x = ( ( block8x8%2 )<<3 );
				boff_y = ( ( block8x8/2 )<<3 );
				shift = IQ_SHIFT[qp]-1;
				QPI   = IQ_TAB[qp];
				currMB->y_4x4_dc[block8x8/2][block8x8%2][b4/2][b4%2] = 0;
				if(currMB->b4_ref[block8x8][block4x4]==INTRA)
				{
					switch ( block8x8*4+b4 )
					{
					case 0:
						if ( img->pix_x && mb_data[img->current_mb_nr-1].mb_type==I8MB )
						{
							currMB->y_4x4_dc[block8x8/2][block8x8%2][b4/2][b4%2]=mb_data[img->current_mb_nr-1].y_dc[0][1]>>1;
						}
						else if( img->pix_y && mb_data[img->current_mb_nr-( img->width/MB_BLOCK_SIZE )].mb_type==I8MB )
						{
							currMB->y_4x4_dc[block8x8/2][block8x8%2][b4/2][b4%2]=mb_data[img->current_mb_nr-( img->width/MB_BLOCK_SIZE )].y_dc[1][0]>>1;
						}
						break;

					case 1:
					case 2:
					case 3:
					case 5:
					case 6:
					case 7:
					case 9:
					case 10:
					case 11:
					case 13:
					case 14:
					case 15:
						currMB->y_4x4_dc[block8x8/2][block8x8%2][b4/2][b4%2] = currMB->y_4x4_dc[block8x8/2][block8x8%2][0][0];
						break;
					case 4:
					case 8:
					case 12:
						currMB->y_4x4_dc[block8x8/2][block8x8%2][b4/2][b4%2] = currMB->y_dc[0][0]>>1;
						break;
					}
				}
                val=currMB->y_4x4_dc[block8x8/2][block8x8%2][b4/2][b4%2];
                sum = sign( ( RFM_ABS( val )*QPI+( 1<<( shift-1 ) ) )>>shift,val );
                img->m7[boff_x+( b4%2 )*4][boff_y+( b4/2 ) *4]=sum;
#endif
			}
        } //end 4x4
        {
            ///Should be changed
            tmp_y_dc = 0;
            for ( k=0; k<2; k++ )
                for ( m=0; m<2; m++ )
                    tmp_y_dc += currMB->y_4x4_dc[block8x8/2][block8x8%2][k][m];
            currMB->y_dc[block8x8/2][block8x8%2] = ( tmp_y_dc+1 )/2; //4;
        }
        if( any_coeff )
        {
            cbp_blk_mask = cbp_blk_masks[ ( ( boff_y&4 )>>1 )|( ( boff_x&4 )>>2 ) ] ;
            if( block8x8&1 )cbp_blk_mask<<=2;
            if( block8x8&2 )cbp_blk_mask<<=8;

            currMB->cbp_blk |= cbp_blk_mask;
        }

    }//end 4x4 dct
}

void read_chroma_coeff_b8_qcoder( int block8x8, input_t* inp, image_t* img )
{
    int mb_nr = img->current_mb_nr;
    macroblock_t *currMB = &mb_data[mb_nr];
    int intra;
    int inumcoeff;                /* number of coeffs per block */
    int ipos;
    int ii, jj, i;
    int boff_x, boff_y;
    int any_coeff;
    int vlc_numcoef;
    int buffer_coeff[64];

    float QP99;
    int QPI;
    int shift = 7;
    int val;
    int qp_chroma = QP_SCALE_CR[currMB->qp];  ////ChromaQP [9/17/2011 Will]
    long sum;
    int is_eob = 0;

    //for DC prediction
    int readDC=0;

    memset( buffer_coeff, 0, 64*sizeof( int ) );
    QP99 = ( float )( pow( 2.0, ( float )( qp_chroma - 5 ) / 8.0 ) );

    for ( shift = 1; shift < 16; shift++ )
    {
        QPI = ( int )( ( 1 << shift ) * QP99 + 0.5 );
        if ( QPI > ( 1 << 16 ) )
        {
            shift -= 1;
            QPI = ( int )( ( 1 << shift ) * QP99 + 0.5 );
            break;
        }
    }

    if ( shift == 16 )
        shift = 15;
    QPI = ( int )( ( 1 << shift ) * QP99 + 0.5 );

    intra = IS_INTRA( currMB );
    inumcoeff = 64;               //  all positions + EOB

    //clear cbp_blk bits of thie 8x8 block (and not all 4!)
    vlc_numcoef = -1;

    if ( intra )
    {
        vlc_numcoef = 0;            //this means 'use numcoeffs symbol'.
    }

    // === set offset in current macroblock ===
    boff_x = ( ( block8x8 % 2 ) << 3 );
    boff_y = ( ( block8x8 / 2 ) << 3 );
    img->subblock_x = boff_x >> 2;
    img->subblock_y = boff_y >> 2;

    ipos = -1;
    any_coeff = 1;

    for ( i = 0; i < inumcoeff; )
    {
        is_eob = qcoder_decode_eobflag_chroma( qcoder, i );
        if( !is_eob )
        {
            do
            {
                buffer_coeff[i] = qcoder_decode_chroma_b8x8_coeff( qcoder, i, intra );
                i++;
            }
            while( buffer_coeff[i-1]==0 );
        }
        else
            break;  // EOB reached
    }

    vlc_numcoef = i;

    if( vlc_numcoef !=0 )
    {
        for( i=0; i<vlc_numcoef; i++ )
        {
            ipos = i;
            if( buffer_coeff[i]==0 )
                continue;
            if( ipos>=64 )
                break;

            ii = SCAN[1][ipos][0];
            jj = SCAN[1][ipos][1];
            shift = IQ_SHIFT[qp_chroma];
            QPI = IQ_TAB[qp_chroma];
            val = buffer_coeff[i];

            //////////////
            if( !ii && !jj )
            {
#if INTRA_MODE_ADAPTIVE_CHROMA
                int uv=block8x8-4;
                if( currMB->uvmode==INTRA_UP )
                {
                    val+=mb_data[img->current_mb_nr-( img->width/MB_BLOCK_SIZE )].uv_dc[uv];
                }
                else if( currMB->uvmode==INTRA_LEFT )
                {
                    val+=mb_data[img->current_mb_nr-1].uv_dc[uv];
                }
#else
				int uv=block8x8-4;
				if(img->pix_x && mb_data[img->current_mb_nr-1].mb_type==I8MB )
				{
					val+=mb_data[img->current_mb_nr-1].uv_dc[uv];
				}
				else if(img->pix_y && mb_data[img->current_mb_nr-(img->width/MB_BLOCK_SIZE)].mb_type==I8MB )
				{
					val+=mb_data[img->current_mb_nr-(img->width/MB_BLOCK_SIZE)].uv_dc[uv];
				}
#endif
                currMB->uv_dc[uv]=val;
                readDC=1;
            }

            if ( val > 2047 )
            {
                CSyntax = 0;
#if SYNTAX_REPORT
                fprintf( p_sreport,"error(1)                %8d @(%d,%d) Blk%d MB%d\n",val,jj,ii,block8x8,1/*img->picture_structure*/==0
                         ?img->current_mb_nr_fld:img->current_mb_nr );
#endif
                val = 2047;
            }
            else if ( val < -2048 )
            {
                CSyntax = 0;
#if SYNTAX_REPORT
                fprintf( p_sreport,"error(1)                %8d @(%d,%d) Blk%d MB%d\n",val,jj,ii,block8x8,1/*img->picture_structure*/==0
                         ?img->current_mb_nr_fld:img->current_mb_nr );
#endif
                val = -2048;
            }
            sum = sign( ( RFM_ABS( val ) * QPI + ( 1 << ( shift - 2 ) ) ) >> ( shift - 1 ),val );

            if ( sum > 8191 )
            {
                CSyntax = 0;
#if SYNTAX_REPORT
                fprintf( p_sreport,"error(2)                %8d @(%d,%d) Blk%d MB%d\n",sum,jj,ii,block8x8,1/*img->picture_structure*/==0
                         ?img->current_mb_nr_fld:img->current_mb_nr );
#endif
                sum = 8191;
            }
            else if ( sum < -8192 )
            {
                CSyntax = 0;
#if SYNTAX_REPORT
                fprintf( p_sreport,"error(2)                %8d @(%d,%d) Blk%d MB%d\n",sum,jj,ii,block8x8,1/*img->picture_structure*/==0
                         ?img->current_mb_nr_fld:img->current_mb_nr );
#endif
                sum = -8192;
            }

            img->m8[block8x8 - 4][ii][jj] = sum;
        }

        //add for DC prediction
        if( !readDC )
        {
            int uv=block8x8-4;
            currMB->uv_dc[uv]=0;
#if INTRA_MODE_ADAPTIVE_CHROMA
            if( currMB->uvmode==INTRA_UP )
            {
                currMB->uv_dc[uv]=mb_data[img->current_mb_nr-( img->width/MB_BLOCK_SIZE )].uv_dc[uv];
            }
            else if( currMB->uvmode==INTRA_LEFT )
            {
                currMB->uv_dc[uv]=mb_data[img->current_mb_nr-1].uv_dc[uv];
            }
#else
			if(img->pix_x && mb_data[img->current_mb_nr-1].mb_type==I8MB )
			{
				currMB->uv_dc[uv]=mb_data[img->current_mb_nr-1].uv_dc[uv];
			}
			else if(img->pix_y && mb_data[img->current_mb_nr-(img->width/MB_BLOCK_SIZE)].mb_type==I8MB )
			{
				currMB->uv_dc[uv]=mb_data[img->current_mb_nr-(img->width/MB_BLOCK_SIZE)].uv_dc[uv];
			}
#endif
            val=currMB->uv_dc[uv];
            sum = sign( ( RFM_ABS( val ) * QPI + ( 1 << ( shift - 2 ) ) ) >> ( shift - 1 ),val );
            img->m8[uv][0][0]=sum;
        }
    }
    else
    {
        int uv=block8x8-4;
        shift = IQ_SHIFT[qp_chroma];
        QPI = IQ_TAB[qp_chroma];
        currMB->uv_dc[uv]=0;
#if INTRA_MODE_ADAPTIVE_CHROMA
        if( currMB->uvmode==INTRA_UP )
        {
            currMB->uv_dc[uv]=mb_data[img->current_mb_nr-( img->width/MB_BLOCK_SIZE )].uv_dc[uv];
        }
        else if( currMB->uvmode==INTRA_LEFT )
        {
            currMB->uv_dc[uv]=mb_data[img->current_mb_nr-1].uv_dc[uv];
        }
#else
		if(img->pix_x && mb_data[img->current_mb_nr-1].mb_type==I8MB )
		{
			currMB->uv_dc[uv]=mb_data[img->current_mb_nr-1].uv_dc[uv];
		}
		else if(img->pix_y && mb_data[img->current_mb_nr-(img->width/MB_BLOCK_SIZE)].mb_type==I8MB )
		{
			currMB->uv_dc[uv]=mb_data[img->current_mb_nr-(img->width/MB_BLOCK_SIZE)].uv_dc[uv];
		}
#endif
        val=currMB->uv_dc[uv];
        sum = sign( ( RFM_ABS( val ) * QPI + ( 1 << ( shift - 2 ) ) ) >> ( shift - 1 ),val );
        img->m8[uv][0][0]=sum;
    }
    //////////////

    if ( any_coeff )
    {
        currMB->cbp_blk |= 0xf0000 << ( ( block8x8 - 4 ) << 2 );
    }
    return;
}

/*
*************************************************************************
* Function:Copy region of img->m7 corresponding to block8x8 to curr_blk[][].
* Input:
* Output:
* Return:
* Attention:img->m7 is [x][y] and curr_blk is [y][x].
*************************************************************************
*/

void get_curr_blk( int block8x8, image_t *img, int curr_blk[B8_SIZE][B8_SIZE] )
{
    int xx, yy;
    int mb_y = ( block8x8 / 2 ) << 3;
    int mb_x = ( block8x8 % 2 ) << 3;

    for ( yy = 0; yy < B8_SIZE; yy++ )
        for ( xx = 0; xx < B8_SIZE; xx++ )
        {
            if ( block8x8 <= 3 )
                curr_blk[yy][xx] = img->m7[mb_x + xx][mb_y + yy];
            else
                curr_blk[yy][xx] = img->m8[block8x8 - 4][xx][yy];
        }
}

void get_curr_blk_16x16( image_t *img, int curr_blk[MB_BLOCK_SIZE][MB_BLOCK_SIZE] )
{
    int xx, yy;
    for ( yy = 0; yy < MB_BLOCK_SIZE; yy++ )
        for ( xx = 0; xx < MB_BLOCK_SIZE; xx++ )
        {
            curr_blk[yy][xx] = img->m7[xx][yy];
        }
}


void Dequant_B16( int qp, int curr_blk[16][16] )
{
    int i, j;
    int val;
    int shift, QPI;

    for ( j=0; j<16; j++ )
    {
        for ( i=0; i<16; i++ )
        {
            val  = curr_blk[j][i];
            shift = IQ_SHIFT[qp];
            QPI   = IQ_TAB[qp];

            curr_blk[j][i] = sign( ( RFM_ABS( val )*QPI+( 1<<( shift-2 ) ) )>>( shift-1 ),val );

        }
    }
    return;
}


void inv_transform_B16( int curr_blk[16][16] )
{
    int xx, i, j ;
    int temp1[16][16] ,temp2[16][16] ;

    for( i=0; i<16; i++ )
    {
        for( j=0; j<16; j++ )
        {
            temp1[i][j] = 0 ;
            for( xx=0; xx<16; xx++ )
            {
                temp1[i][j] += DCT_16[xx][i]*curr_blk[xx][j] ;
            }
        }
    }

    for( i=0; i<16; i++ )
    {
        for( j=0; j<16; j++ )
        {
            temp2[i][j] = 0 ;
            for( xx=0; xx<16; xx++ )
            {
                temp2[i][j] += temp1[i][xx]*DCT_16[xx][j] ;
            }
        }
    }

    for( i=0; i<16; i++ )
    {
        for( j=0; j<16; j++ )
        {
            curr_blk[i][j] = sign( ( RFM_ABS( temp2[i][j] ) + ( 1<<14 ) )>>15,temp2[i][j] ) ;
        }
    }

}



void inv_transform_B4( int curr_blk[B4_SIZE][B4_SIZE] )
{
    int i, j ;
    int temp1[4], temp ;

    for( i=0; i<4; i++ )
    {
        temp1[0] = curr_blk[i][0] + curr_blk[i][2] ;
        temp1[2] = curr_blk[i][0] - curr_blk[i][2] ;
        temp     = ( curr_blk[i][1] + curr_blk[i][3] )*69>>7 ;
        temp1[1] = temp + ( curr_blk[i][1]*98>>7 ) ;
        temp1[3] = temp - ( curr_blk[i][3]*236>>7 ) ;
        //temp1[1] = (curr_blk[i][1]*167>>7) + (curr_blk[i][3]*69>>7) ;
        //temp1[3] = (curr_blk[i][1]*69>>7) - (curr_blk[i][3]*167>>7) ;

        curr_blk[i][0] = temp1[0] + temp1[1] ;
        curr_blk[i][3] = temp1[0] - temp1[1] ;
        curr_blk[i][1] = temp1[2] + temp1[3] ;
        curr_blk[i][2] = temp1[2] - temp1[3] ;
    }
    for( i=0; i<4; i++ )
    {
        temp1[0] = curr_blk[0][i] + curr_blk[2][i] ;
        temp1[2] = curr_blk[0][i] - curr_blk[2][i] ;
        temp     = ( curr_blk[1][i] + curr_blk[3][i] )*69>>7 ;
        temp1[1] = temp + ( curr_blk[1][i]*98>>7 ) ;
        temp1[3] = temp - ( curr_blk[3][i]*236>>7 ) ;

        curr_blk[0][i] = temp1[0] + temp1[1] ;
        curr_blk[3][i] = temp1[0] - temp1[1] ;
        curr_blk[1][i] = temp1[2] + temp1[3] ;
        curr_blk[2][i] = temp1[2] - temp1[3] ;

        for( j=0; j<4; j++ )
        {
            if( curr_blk[j][i]>0 )
            {
                curr_blk[j][i] = ( curr_blk[j][i]+4 )>>3 ;
            }
            else
            {
                curr_blk[j][i] = -( ( -curr_blk[j][i]+4 )>>3 ) ;
            }
        }
    }

}


int inv_transform_B8inB4( int blk[B8_SIZE][B8_SIZE], int block8x8 )
{
    int y,x,yy,xx,block4x4;
    int curr_blk_4x4[4][4][4];

    for( block4x4=0; block4x4<4; block4x4++ )
    {
        for( y=0; y<4; y++ )
        {
            for( x=0; x<4; x++ )
            {
                yy = y + 4*( block4x4/2 );
                xx = x + 4*( block4x4%2 );
                curr_blk_4x4[block4x4][y][x] = blk[yy][xx];
            }
        }
    }

    for( block4x4=0; block4x4<4; block4x4++ )
    {
        inv_transform_B4( curr_blk_4x4[block4x4] );
    }

    for( block4x4=0; block4x4<4; block4x4++ )
    {
        for( y=0; y<4; y++ )
        {
            for( x=0; x<4; x++ )
            {
                yy = y + 4*( block4x4/2 );
                xx = x + 4*( block4x4%2 );
                blk[yy][xx] = curr_blk_4x4[block4x4][y][x];
            }
        }
    }
    return 1;
}

